home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / gcc270cp.lha / gnu / lib / g++-include / BitSet.h < prev    next >
C/C++ Source or Header  |  1995-07-28  |  9KB  |  369 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #ifndef _BitSet_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23.  
  24. #define _BitSet_h 1
  25.  
  26. #include <iostream.h>
  27. #include <limits.h>
  28.  
  29. #undef OK
  30.  
  31. #define BITSETBITS  (sizeof(short) * CHAR_BIT)
  32.  
  33. struct BitSetRep
  34. {
  35.   unsigned short  len;          // number of shorts in s
  36.   unsigned short  sz;           // allocated slots
  37.   unsigned short  virt;         // virtual 0 or 1
  38.   unsigned short  s[1];         // bits start here
  39. };
  40.  
  41. extern BitSetRep*   BitSetalloc(BitSetRep*, const unsigned short*, 
  42.                                 int, int, int);
  43. extern BitSetRep*   BitSetcopy(BitSetRep*, const BitSetRep*);
  44. extern BitSetRep*   BitSetresize(BitSetRep*, int);
  45. extern BitSetRep*   BitSetop(const BitSetRep*, const BitSetRep*, 
  46.                              BitSetRep*, char);
  47. extern BitSetRep*   BitSetcmpl(const BitSetRep*, BitSetRep*);
  48.  
  49.  
  50. extern BitSetRep    _nilBitSetRep;
  51.  
  52. class BitSet;
  53.  
  54. class BitSetBit
  55. {
  56. protected:
  57.   BitSet*            src;
  58.   unsigned long      pos;
  59.  
  60.  public:
  61.                      BitSetBit(BitSet* v, int p);
  62.                      BitSetBit(const BitSetBit& b);
  63.                     ~BitSetBit();
  64.                      operator int() const;
  65.   int                operator = (int b);
  66.   int                operator = (const BitSetBit& b);
  67. };
  68.  
  69. class BitSet
  70. {
  71. protected:
  72.   BitSetRep*          rep;
  73.  
  74.   
  75. public:
  76.  
  77. // constructors
  78.                      BitSet();
  79.                      BitSet(const BitSet&);
  80.  
  81.                     ~BitSet();
  82.  
  83.   BitSet&            operator =  (const BitSet& y);
  84.  
  85. // equality & subset tests
  86.  
  87.   friend int         operator == (const BitSet& x, const BitSet& y);
  88.   friend int         operator != (const BitSet& x, const BitSet& y);
  89.   friend int         operator <  (const BitSet& x, const BitSet& y);
  90.   friend int         operator <= (const BitSet& x, const BitSet& y);
  91.   friend int         operator >  (const BitSet& x, const BitSet& y);
  92.   friend int         operator >= (const BitSet& x, const BitSet& y);
  93.  
  94.  
  95. // operations on self
  96.  
  97.   BitSet&            operator |= (const BitSet& y);
  98.   BitSet&            operator &= (const BitSet& y);
  99.   BitSet&            operator -= (const BitSet& y);
  100.   BitSet&            operator ^= (const BitSet& y);
  101.  
  102.   void               complement();
  103.  
  104. // individual bit manipulation
  105.  
  106.   void               set(int pos);
  107.   void               set(int from, int to);
  108.   void               set(); // set all
  109.  
  110.   void               clear(int pos);
  111.   void               clear(int from, int to);
  112.   void               clear(); // clear all
  113.  
  114.   void               invert(int pos);
  115.   void               invert(int from, int to);
  116.  
  117.   int                test(int pos) const;
  118.   int                test(int from, int to) const;
  119.  
  120.   BitSetBit          operator [] (int i);
  121.   
  122. // iterators
  123.  
  124.   int                first(int b = 1) const;
  125.   int                last(int b = 1) const;
  126.  
  127.   int                next(int pos, int b = 1) const;
  128.   int                prev(int pos, int b = 1) const;
  129.   int                previous(int pos, int b = 1) const /* Obsolete synonym */
  130.     { return prev(pos, b); }
  131.  
  132. // status
  133.  
  134.   int                empty() const;
  135.   int                virtual_bit() const;
  136.   int                count(int b = 1) const;
  137.   
  138. // convertors & IO
  139.  
  140.   friend BitSet      atoBitSet(const char* s, 
  141.                                char f='0', char t='1', char star='*');
  142.   // BitSettoa is deprecated; do not use in new programs.
  143.   friend const char* BitSettoa(const BitSet& x, 
  144.                                char f='0', char t='1', char star='*');
  145.  
  146.   friend BitSet      shorttoBitSet(unsigned short w);
  147.   friend BitSet      longtoBitSet(unsigned long w);
  148.  
  149.   friend ostream&    operator << (ostream& s, const BitSet& x);
  150.   void             printon(ostream& s,
  151.                  char f='0', char t='1', char star='*') const;
  152.  
  153. // procedural versions of operators
  154.  
  155.   friend void        and(const BitSet& x, const BitSet& y, BitSet& r);
  156.   friend void        or(const BitSet& x, const BitSet& y, BitSet& r);
  157.   friend void        xor(const BitSet& x, const BitSet& y, BitSet& r);
  158.   friend void        diff(const BitSet& x, const BitSet& y, BitSet& r);
  159.   friend void        complement(const BitSet& x, BitSet& r);
  160.  
  161. // misc
  162.  
  163.   void      error(const char* msg) const;
  164.   int                OK() const;
  165. };
  166.  
  167.  
  168. typedef BitSet BitSetTmp;
  169.  
  170. // These are inlined regardless of optimization
  171.  
  172. inline int BitSet_index(int l)
  173. {
  174.   return (unsigned)(l) / BITSETBITS;
  175. }
  176.  
  177. inline int BitSet_pos(int l)
  178. {
  179.   return l & (BITSETBITS - 1);
  180. }
  181.  
  182.  
  183. inline BitSet::BitSet() : rep(&_nilBitSetRep) {}
  184.  
  185. inline BitSet::BitSet(const BitSet& x) :rep(BitSetcopy(0, x.rep)) {}
  186.  
  187. inline BitSet::~BitSet() { if (rep != &_nilBitSetRep) delete rep; }
  188.  
  189. inline BitSet& BitSet::operator =  (const BitSet& y)
  190.   rep = BitSetcopy(rep, y.rep);
  191.   return *this;
  192. }
  193.  
  194. inline int operator != (const BitSet& x, const BitSet& y) { return !(x == y); }
  195.  
  196. inline int operator >  (const BitSet& x, const BitSet& y) { return y < x; }
  197.  
  198. inline int operator >= (const BitSet& x, const BitSet& y) { return y <= x; }
  199.  
  200. inline void and(const BitSet& x, const BitSet& y, BitSet& r)
  201. {
  202.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '&');
  203. }
  204.  
  205. inline void or(const BitSet& x, const BitSet& y, BitSet& r)
  206. {
  207.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '|');
  208. }
  209.  
  210. inline void xor(const BitSet& x, const BitSet& y, BitSet& r)
  211. {
  212.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '^');
  213. }
  214.  
  215. inline void diff(const BitSet& x, const BitSet& y, BitSet& r)
  216. {
  217.   r.rep =  BitSetop(x.rep, y.rep, r.rep, '-');
  218. }
  219.  
  220. inline void complement(const BitSet& x, BitSet& r)
  221. {
  222.   r.rep = BitSetcmpl(x.rep, r.rep);
  223. }
  224.  
  225. #if defined(__GNUG__) && !defined(_G_NO_NRV)
  226.  
  227. inline BitSet operator & (const BitSet& x, const BitSet& y) return r
  228. {
  229.   and(x, y, r);
  230. }
  231.  
  232. inline BitSet operator | (const BitSet& x, const BitSet& y) return r
  233. {
  234.   or(x, y, r);
  235. }
  236.  
  237. inline BitSet operator ^ (const BitSet& x, const BitSet& y) return r
  238. {
  239.   xor(x, y, r);
  240. }
  241.  
  242. inline BitSet operator - (const BitSet& x, const BitSet& y) return r
  243. {
  244.   diff(x, y, r);
  245. }
  246.  
  247. inline BitSet operator ~ (const BitSet& x) return r
  248. {
  249.   ::complement(x, r);
  250. }
  251.  
  252. #else /* NO_NRV */
  253.  
  254. inline BitSet operator & (const BitSet& x, const BitSet& y) 
  255. {
  256.   BitSet r; and(x, y, r); return r;
  257. }
  258.  
  259. inline BitSet operator | (const BitSet& x, const BitSet& y) 
  260. {
  261.   BitSet r; or(x, y, r); return r;
  262. }
  263.  
  264. inline BitSet operator ^ (const BitSet& x, const BitSet& y) 
  265. {
  266.   BitSet r; xor(x, y, r); return r;
  267. }
  268.  
  269. inline BitSet operator - (const BitSet& x, const BitSet& y) 
  270. {
  271.   BitSet r; diff(x, y, r); return r;
  272. }
  273.  
  274. inline BitSet operator ~ (const BitSet& x) 
  275. {
  276.   BitSet r; ::complement(x, r); return r;
  277. }
  278.  
  279. #endif
  280.  
  281. inline BitSet& BitSet::operator &= (const BitSet& y)
  282. {
  283.   and(*this, y, *this);
  284.   return *this;
  285. }
  286.  
  287. inline BitSet& BitSet::operator |= (const BitSet& y)
  288. {
  289.   or(*this, y, *this);
  290.   return *this;
  291. }
  292.  
  293. inline BitSet& BitSet::operator ^= (const BitSet& y)
  294. {
  295.   xor(*this, y, *this);
  296.   return *this;
  297. }
  298.  
  299. inline BitSet& BitSet::operator -= (const BitSet& y)
  300. {
  301.   diff(*this, y, *this);
  302.   return *this;
  303. }
  304.  
  305.  
  306. inline void BitSet::complement()
  307. {
  308.   ::complement(*this, *this);
  309. }
  310.  
  311. inline int BitSet::virtual_bit() const
  312. {
  313.   return rep->virt;
  314. }
  315.  
  316. inline int BitSet::first(int b) const
  317. {
  318.   return next(-1, b);
  319. }
  320.  
  321. inline int BitSet::test(int p) const
  322. {
  323.   if (p < 0) error("Illegal bit index");
  324.   int index = BitSet_index(p);
  325.   return (index >= rep->len)? rep->virt : 
  326.          ((rep->s[index] & (1 << BitSet_pos(p))) != 0);
  327. }
  328.  
  329.  
  330. inline void BitSet::set()
  331. {
  332.   rep = BitSetalloc(rep, 0, 0, 1, 0);
  333. }
  334.  
  335. inline BitSetBit::BitSetBit(const BitSetBit& b) :src(b.src), pos(b.pos) {}
  336.  
  337. inline BitSetBit::BitSetBit(BitSet* v, int p)
  338. {
  339.   src = v;  pos = p;
  340. }
  341.  
  342. inline BitSetBit::~BitSetBit() {}
  343.  
  344. inline BitSetBit::operator int() const
  345. {
  346.   return src->test(pos);
  347. }
  348.  
  349. inline int BitSetBit::operator = (int b)
  350. {
  351.   if (b) src->set(pos); else src->clear(pos); return b;
  352. }
  353.  
  354. inline int BitSetBit::operator = (const BitSetBit& b)
  355. {
  356.   int i = (int)b;
  357.   *this = i;
  358.   return i;
  359. }
  360.  
  361. inline BitSetBit BitSet::operator [] (int i)
  362. {
  363.   if (i < 0) error("illegal bit index");
  364.   return BitSetBit(this, i);
  365. }
  366.  
  367. #endif
  368.